package cn.nfwx.tv.controller;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import cn.nfwx.tv.R;
import cn.nfwx.tv.base.BaseDialogFragment;
import cn.nfwx.tv.util.GlideUtil;
import cn.nfwx.tv.util.SharePrefUtil;

/**
 * 功能引导图
 * Created by dell on 2017/11/29.
 */
public class PlayerIntroductionController extends BaseDialogFragment implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private LinearLayout ll_introdution;
    private boolean isDragPage;
    private boolean isLastPage;
    private boolean canJumpPage;

    private static final String KEY_NOTE_SHOW = "key_note_show";


    public static PlayerIntroductionController newInstance(boolean showNote) {
        PlayerIntroductionController fragment = new PlayerIntroductionController();
        Bundle bundle = new Bundle();
        bundle.putBoolean(KEY_NOTE_SHOW, showNote);
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * 是否填满整个屏幕
     */
    protected boolean isMatchParent() {
        return true;
    }

    protected boolean isOutsideTouchable() {
        return true;
    }

    //是否透明背景
    protected boolean isTranslucentBackground() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_introduction;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        boolean showNote = getArguments().getBoolean(KEY_NOTE_SHOW, true);
        canJumpPage = true;
        if (showNote) {
            ll_introdution.setVisibility(View.VISIBLE);
        } else {
            ll_introdution.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        int[] layouts = new int[]{R.layout.layout_introdution_1, R.layout.layout_introdution_1, R.layout.layout_introdution_1, R.layout.layout_introdution_2};
        int[] mipmaps = new int[]{R.drawable.image_introduction_1, R.drawable.image_introduction_2, R.drawable.image_introduction_3};
        ViewPager vp_introduction = findView(R.id.vp_introduction);
        ll_introdution = findView(R.id.ll_introdution);
        findView(R.id.tv_cancle_btn).setOnClickListener(this);
        findView(R.id.bt_introdution_cancel).setOnClickListener(this);
        findView(R.id.bt_introdution_enter).setOnClickListener(this);
        LinearLayout ll_point = findView(R.id.ll_point);
        List<View> views = new ArrayList<>();

        for (int i = 0; i < layouts.length; i++) {
            int layout = layouts[i];
            View view = LayoutInflater.from(getContext()).inflate(layout, null);
            ImageView imageView = view.findViewById(R.id.iv_introduction);
            if (i == 0 || i == 1 || i == 2) {
                GlideUtil.loadImage(getActivity(), mipmaps[i], imageView);
                //imageView.setImageResource(mipmaps[i]);
            } else {
                view.findViewById(R.id.bt_enter).setOnClickListener(this);
            }
            views.add(view);
        }

        IntroductionViewPagerAdapter adapter = new IntroductionViewPagerAdapter(views);
        vp_introduction.setAdapter(adapter);
        vp_introduction.addOnPageChangeListener(this);

        for (int i = 0; i < layouts.length; i++) {
            ImageView imageView = new ImageView(getContext());
            LinearLayout.LayoutParams l = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            l.setMargins(10, 0, 10, 0);
            imageView.setLayoutParams(l);
            imageView.setImageResource(R.drawable.introdution_point_selecter);
            ll_point.addView(imageView);
            //ivPoints.add(imageView);
        }

        ll_point.getChildAt(0).setSelected(true);
        //ivPoints.get(0).setSelected(true);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_introdution_cancel:
            case R.id.tv_cancle_btn:
            case R.id.bt_enter:
                SharePrefUtil.saveFristIntroduction(getContext(), false);
                dismiss();
                break;
            case R.id.bt_introdution_enter:
                ll_introdution.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (isLastPage && isDragPage && positionOffsetPixels == 0) {
            if (canJumpPage) {
                canJumpPage = false;
                SharePrefUtil.saveFristIntroduction(getContext(), false);
                dismiss();
            }
        }
    }

    @Override
    public void onPageSelected(int position) {
        LinearLayout ll_point = findView(R.id.ll_point);
        for (int i = 0; i < ll_point.getChildCount(); i++) {
            ll_point.getChildAt(i).setSelected(false);
        }
        ll_point.getChildAt(position).setSelected(true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        isDragPage = state == 1;
    }

    public static class IntroductionViewPagerAdapter extends PagerAdapter {

        List<View> mViewList = new ArrayList<>();

        public IntroductionViewPagerAdapter(List<View> viewList) {
            mViewList.clear();
            mViewList.addAll(viewList);
        }

        @Override
        public int getCount() {
            return mViewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViewList.get(position));

            return mViewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViewList.get(position));
        }

        public void clear() {
            mViewList.clear();
        }
    }
}
